<?php

class Admin_BlogLinkController extends Ext_Controller_Action_Admin
{
    public function init()
    {
        parent::init();
    }
    
    public function showListAction()
    {
        $tags = array();
        $linksAll = App_Model_DbTable_Links::getInstance()->getAll();
        for($i=0;$i<count($linksAll);$i++)
        {
            $linkId = $linksAll[$i]['id'];
            
            $linkRow = App_Model_DbTable_Links::getInstance()->getRowById($linkId);

            $links[] = $linkRow;
        }
        $this->view->links = $links;

        $viewForm = new Ext_View_Form_Admin_Blog_Links();
        $this->view->addLinkForm = $viewForm;
    }
    
    public function getLinkForJeditableAction()
    {
        $viewResult = new Ext_View_Result();

        $linkId = $this->filter->filter($this->_request->getPost('linkId')); 
        $column = $this->filter->filter($this->_request->getPost('column')); 
        
        $linkRow = App_Model_DbTable_Links::getInstance()->getRowById($linkId);
        $columnValue = $linkRow[$column];
        
        $viewResult->setNoLayout();
        $viewResult->setNoRender();
        
        echo $columnValue;
    }
    
    public function saveLinkForJeditableAction()
    {
        $viewResult = new Ext_View_Result();
        
        $linkId      = $this->filter->filter($this->_request->getPost('linkId')); 
        $column      = $this->filter->filter($this->_request->getPost('column')); 
        $columnValue = $this->filter->filter($this->_request->getPost('value')); 
        
        $row = array(
            $column => $columnValue,
        );
        App_Model_DbTable_Links::getInstance()->addOld($row, $linkId);

        $linkRow = App_Model_DbTable_Links::getInstance()->getRowById($linkId);
        $columnValue = $linkRow[$column];
        
        $viewResult->setNoLayout();
        $viewResult->setNoRender();
        
        echo $columnValue;
    }
    
    public function saveOrderAction()
    {
        $viewResult = new Ext_View_Result();
        
        $newOrder = $this->_request->getPost('newOrder');
        if($newOrder){
            for($i=0;$i<count($newOrder);$i++)
            {
                $linkId = $newOrder[$i];
                $row = array(
                    'sequence' => $i,
                );
                App_Model_DbTable_Links::getInstance()->addOld($row, $linkId);
            }
        }
        
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function deleteLinkAction()
    {
        $viewResult = new Ext_View_Result();
        
        $linkId = $this->filter->filter($this->_request->getPost('linkId'));

        App_Model_DbTable_Links::getInstance()->del($linkId);
        
        $viewResult->addMessage($this->translater->_("Delete success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function saveLinkAction()
    {
        $viewResult = new Ext_View_Result();
        
        $siteName = $this->filter->filter($this->_request->getPost('siteName'));
        $siteLink = $this->filter->filter($this->_request->getPost('siteLink'));
        
        if(!$siteName){
            $viewResult->addMessage($this->translater->_("Site name is required."));
            $viewResult->setNoRender();
            return "";
        }
        if(!$siteLink){
            $viewResult->addMessage($this->translater->_("Site link is required."));
            $viewResult->setNoRender();
            return "";
        }
        
        $sequence = App_Model_DbTable_Links::getInstance()->getMaxSequence();
        $row = array(
            'siteName' => $siteName,
            'siteLink' => $siteLink,
            'sequence' => $sequence+1,
        );
        $linkId = App_Model_DbTable_Links::getInstance()->addNew($row);

        $link = App_Model_DbTable_Links::getInstance()->getRowById($linkId);
        $this->view->link = $link;

        $viewResult->setResult(1);
        $viewResult->addMessage($this->translater->_("Submit success."));
    }
    
}

